<?php


namespace app\admin\controller;

use think\Db;
use think\Request;

/**
 *
 * @author wuyaorong
 * @since 2023/08/12
 * Class Ad
 * @package app\admin\controller
 */

class Dreamapi extends Backend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    public $appkey = "hangtianxinxi-prod";//apiKey
    public $secret = "cWBLqQVWYM8bmQDX13PLGhZJfKX1EjP3EnbjpVmPAx8CmZnB3g8Sbp79FUZlyQcc";//apiSecret
    public $host = "https://data-sync.dream-sports.cn/";

    /**
     * 初始化
     * @author wuyaorong
     * @since 2023/08/12
     */
    protected function initialize(Request $request = null)
    {
        parent::initialize($request); // TODO: Change the autogenerated stub
        
    }

    /**
     * @return json
     */
    public function post($url, $post_data)
    {
        $timestamp = $this->get_msectime();
        $post_data = json_encode($post_data);
        $nonce = $this->get_rand_str(15);
        $sign = $this->getSign($url, $timestamp, $nonce);
        halt($sign);
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $this->host . $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $post_data,
            CURLOPT_HTTPHEADER => array(
                "Content-Type: application/json",
                "client-key: " . $this->appkey,
                "nonce: ". $nonce,
                "signature: " . $sign,
                "timestamp: " . $timestamp,
                "api-version: V1.3.2"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $response;
    }

    /**
     * 获取签名
     * @param $post_data
     * @param bool $timestamp
     * @return string
     */
    public function getSign($url, $timestamp, $nonce)
    {
        $data = [
            'method' => 'POST',
            'http_path' => $url,
            'timestamp' => $timestamp,
            'nonce' => $nonce
            
        ];
        ksort($data);
        $signString = '';
        foreach ($data as $k => $v) {
            $signString = $signString . "$k=$v&";
        }
        $signString = $signString . "secret=$this->secret";
        $sign = md5($signString);
        //echo "签名原始串:$signString<br/>"."签名后:$sign <br/>";
        return $sign;
    }

    //返回当前的毫秒时间戳
    public function get_msectime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }


    //随机字符串
    public function get_rand_str($len){
        $str = "1234567890asdfghjklqwertyuiopzxcvbnmASDFGHJKLZXCVBNMPOIUYTREWQ";
        return substr(str_shuffle($str),0,$len);
    }

    //获取同步运动结果
    public function synchronous($str)
    {
        halt($str);
        $res = json_decode($this->post('/api/isport-data/sync-sport-result', $str), true);
        return $res;
        
    }
}